/**
 * Created by frank on 2016/9/6.
 */
(function () {
    'use strict';

    angular
        .module('user.login.controller', [])
        .controller('UserLoginCtrl', UserLoginCtrl);

    UserLoginCtrl.$inject = ['$scope','$yikeUtils','$rootScope','$state','$ionicHistory','$ionicModal','localStorageService','$ionicLoading','$rootScope'];
    /* @ngInject */
    function UserLoginCtrl($scope,$yikeUtils,$rootScope,$state,$ionicHistory,$ionicModal,localStorageService,$ionicLoading){
        $scope.isLogin = 1;
        $scope.user={
            email:'',
            password:'',
            op:'',
            qq:""
        };
        $scope.openQQ=openQQ;
        $scope.isOpen=window.isOpen;
        var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
        $scope.login=login;
        $scope.focusStyle = focusStyle;
        $scope.blurStyle = blurStyle;
        $scope.focusStyleState = 1;
        $scope.loginOrRegister = loginOrRegister;
        $scope.$on('$ionicView.beforeEnter', function() {
            if(localStorageService.get('user')){
                $state.go('tab.home')
            }
            if(localStorageService.get('account')){
                $scope.user={
                    email:localStorageService.get('account').email,
                    password:localStorageService.get('account').password
                };
            }
        });
        init();
        function init() {
            //是否显示充值等信息
            // yikeTaishan.isShowRecharge()
            //     .then(function (data) {
            //         $scope.isOpen=data.result.open;
            //     });
            // //获取客服微信,qq
            // yikeTaishan.personalCenter('platform','')
            //     .then(function (data) {
            //         if(data.status == 1){
            //             $scope.message=data.result;
            //             $scope.$digest();
            //         }
            //     })
            /*获取qq客服*/
            //yikeTaishan.QQkf()
            //    .then(function(data){
            //        $scope.user.qq = data.result.result;
            //    }).catch(function(err){
            //    console.log(err);
            //})
        }
        /*是登录还是注册*/
        function loginOrRegister(status){
            $scope.isLogin = status;
        }
        //联系客服
        $ionicModal.fromTemplateUrl('templates/modal/service.html', {
            scope: $scope,
            animation: 'slide-in-up'
        }).then(function(modal) {
            $scope.service= modal;
        });
        $scope.openModal = function() {
            $scope.service.show();
        };
        $scope.closeModal = function() {
            $scope.service.hide();
        };
        $scope.$on('$destroy', function () {
            $scope.service.remove();
        });
        //客服QQ
        function openQQ(){
            cordova.InAppBrowser.open('mqqwpa://im/chat?chat_type=wpa&uin='+$scope.user.qq+'&version=1&src_type=web&web_src=oicqzone.com','_system','location=no,toolbar=yes,toolbarposition=top,closebuttoncaption=关闭')
        }

        /*获得焦点改变*/
        function focusStyle(){
            $scope.focusStyleState = 0;
        }
        /*失去焦点*///这里用键盘的隐藏事件
        /*window.addEventListener('native.keyboardhide', function (e) {
         console.log("能否监听键盘隐藏事件");
         $scope.focusStyleState = 1;
         });*/
        /*window.addEventListener('native.keyboardshow', function (e) {
         //e.keyboardHeight 表示软件盘显示的时候的高度
         $scope.focusStyleState = 0;
         });*/
        function blurStyle(){
            $scope.focusStyleState = 1;
        }

        //表单验证
        function formValidation() {
            if($scope.user.email == '' || $scope.user.email == null){
                $yikeUtils.toast('请先输入帐号');
                return false;
            }else if($scope.user.password == '' || $scope.user.password == null){
                $yikeUtils.toast('请先输入密码');
                return false;
            }else{
                return true;
            }
        }
        //登录
        function login() {
            //console.log(12312);
            //$state.go('tab.home');
            var suc=formValidation();
            if(suc){
                $ionicLoading.show({
                    template: '<ion-spinner icon="bubbles"></ion-spinner>'
                });
                if(filter.test($scope.user.email)){
                    $scope.user.op='email';
                }else {
                    $scope.user.op='phone';
                }
                yikeTaishan.login($scope.user.email,$scope.user.password,$scope.user.op)
                    .then(function (data) {
                        console.log(data);
                        $yikeUtils.toast(data.result.result);
                        if(data.status == 1){
                            $scope.alluser = {
                                id:data.result.user.id,
                                token:data.result.user.token
                            };
                            localStorageService.set('alluser',$scope.alluser);
                            localStorageService.set('user',data.result.user);
                            localStorageService.set('account',$scope.user);
                            $state.go('tab.home');
                        }
                    })

            }
        }
        // 游客模式
        $scope.tourist=function () {
            localStorageService.set('user',{token:'2c992eec9acde07fa81bf18c2aac8133'});
            $state.go('tab.home');
        };

        /*注册的逻辑*/
        $scope.user1={
            phone:'',
            qq:'',
            name:'',
            password:'',
            passwordTwo:'',
            op:'register',
            msg:'',
            code:''
        };
        $scope.register=register;
        $scope.sendMsg=sendMsg;
        $scope.focusStyleState = 1;
        //表单验证
        function formValidation1() {
            if($scope.user1.phone == '' || $scope.user1.phone == null){
                $yikeUtils.toast('请先输入手机号');
                return false;
            }else if($scope.user1.code == '' || $scope.user1.code == null){
                $yikeUtils.toast('请先输入验证码');
                return false;
            }else if($scope.user1.password == '' || $scope.user1.password==null){
                $yikeUtils.toast('请先输入密码');
                return false;
            }else if($scope.user1.password.length < 6){
                $yikeUtils.toast('密码长度至少6位');
                return false;
            }else if($scope.user1.passwordTwo == '' || $scope.user1.passwordTwo == null){
                $yikeUtils.toast('请再次输入密码');
                return false;
            }else if($scope.user1.passwordTwo != $scope.user1.password ){
                $yikeUtils.toast('两次密码不一致');
                return false;
            }else if($scope.user1.phone != $scope.user1.msg.phone){
                $yikeUtils.toast('请输入正确的验证码');
                return false;
            }else if($scope.user1.code != $scope.user1.msg.code){
                $yikeUtils.toast('请输入正确的验证码');
                return false;
            }else{
                return true;
            }
        }

        //发送短信验证码
        function sendMsg() {
            if($scope.user1.phone == '' || $scope.user1.phone==null){
                $yikeUtils.toast('请先输入手机号');
                return false;
            }
            yikeTaishan.sendMsg($scope.user1.phone,$scope.user1.op)
                .then(function (data) {
                    console.log(data);
                    $yikeUtils.toast(data.result.result);
                    if(data.status == 1){
                        $scope.user1.msg=data.result.msg;
                        var sendMsg=document.body.querySelector('#send-msg');
                        settime(sendMsg);
                    }
                });
        }
        var countdown=60;
        //倒计时
        function settime(obj) {
            if (countdown == 0) {
                obj.removeAttribute("disabled");
                obj.innerHTML="获取验证码";
                countdown = 60;
                return;
            } else {
                obj.setAttribute("disabled", true);
                obj.innerHTML="重新发送(" + countdown + ")";
                countdown--;
            }
            setTimeout(function() {
                    settime(obj) }
                ,1000)
        }
        //注册
        function register() {
            var suc=formValidation1();
            if(suc){
                $ionicLoading.show({
                    template: '<ion-spinner icon="bubbles"></ion-spinner>'
                });
                yikeTaishan.register($scope.user1.phone,'',$scope.user1.qq,$scope.user1.name,$scope.user1.password,$scope.user1.op)
                    .then(function (data) {
                        $yikeUtils.toast(data.result.result);
                        $scope.isLogin = 1;
                        $scope.user.email = $scope.user1.phone;
                        $scope.user1.phone = "";
                        $scope.user1.code = "";
                        $scope.user1.password = "";
                        $scope.user1.passwordTwo = "";
                    })
            }
        }
    }
})();
